﻿#ifndef RTCSERVICE_H
#define RTCSERVICE_H

#include <QObject>
#include <QLocalServer>
#include <QLocalSocket>
#include <QByteArray>
#include <QDataStream>
#include <QString>
#include <unordered_map>
#include "RTCDefine.h"
#include <engine_interface.h>

namespace RTC3 = ding::rtc;

class AliRtcEventHandler;

// 返回 bool 值的宏（防止越界）
#define RETURN_BOOL(request) \
    ((request.parameters.size() > 0) ? (request.parameters.at(0).toBool()) : (qWarning() << "Index 0 out of range for parameters, returning false.", false))

// 返回 int 值的宏（防止越界）
#define RETURN_INT(request) \
    ((request.parameters.size() > 0) ? (request.parameters.at(0).toInt()) : (qWarning() << "Index 0 out of range for parameters, returning 0.", 0))

// 返回 QString 值的宏（防止越界）
#define RETURN_STRING(request) \
    ((request.parameters.size() > 0) ? (request.parameters.at(0).toString()) : (qWarning() << "Index 0 out of range for parameters, returning empty string.", QString("")))

// 返回 bool 值的宏，带索引参数（防止越界）
#define RETURN_BOOL_INDEX(request, index) \
    (((index) >= 0 && (index) < request.parameters.size()) ? (request.parameters.at(index).toBool()) : (qWarning() << "Index" << index << "out of range for parameters, returning false.", false))

// 返回 int 值的宏，带索引参数（防止越界）
#define RETURN_INT_INDEX(request, index) \
    (((index) >= 0 && (index) < request.parameters.size()) ? (request.parameters.at(index).toInt()) : (qWarning() << "Index" << index << "out of range for parameters, returning 0.", 0))

// 返回 QString 值的宏，带索引参数（防止越界）
#define RETURN_STRING_INDEX(request, index) \
    (((index) >= 0 && (index) < request.parameters.size()) ? (request.parameters.at(index).toString()) : (qWarning() << "Index" << index << "out of range for parameters, returning empty string.", QString("")))


class RTCService : public QObject {
    Q_OBJECT

public:
    explicit RTCService(QObject *parent = nullptr, const QString &serverName = "");
    ~RTCService();
    

    
public slots:
    void processCallback(RTCEngineCallback);
    
    // 设置MMService引用，用于转发回调
    void setMMService(QObject* mmService) { m_mmService = mmService; }
    
private slots:
    void onNewConnection();
    void onReadyRead();
    void onDisconnected();

signals:
    void exit();

private:
    QByteArray buffer;
    void processBuffer(QLocalSocket *socket);
    
    QLocalServer *server = nullptr;
    QLocalSocket* muteSocket = nullptr;
    QVector<QLocalSocket*> sockets;

    QByteArray serializeResponse(const RTCEngineResponse &response,int flag = 1) const;
    RTCEngineRequest deserializeRequest(const QByteArray &requestData) const;
    void processRequest(QLocalSocket *socket, const RTCEngineRequest &request,RTCEngineResponse&);
    QByteArray serializeCallback(const RTCEngineCallback &callback, int flag = 2) const;

    // MMService引用，用于转发回调
    QObject* m_mmService = nullptr;

    RTC3::RtcEngine* engine = nullptr;

    RTC3::RtcEngineAudioDeviceManager* audioDeviceManager = nullptr;
    RTC3::RtcEngineVideoDeviceManager* videoDeviceManager = nullptr;
    RTC3::RtcEngineAudioMixingManager* audioMixingManager = nullptr;

    AliRtcEventHandler *eventhandler = nullptr;
};

#endif // RTCSERVICE_H
